/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ensisa.sunspot.event;

import fr.ensisa.sunspot.Logger;

/**
 *
 * @author Michel Hassenforder
 */

public class StateMachineEvent {

    static public int nextState (int intensity, IntegerEvent oldEvent) {
        int oldIntensity = oldEvent.getIntensity();
        Logger.getLogger().debugln ("StateMachineEvent", "oldEvent ", oldEvent);
        Logger.getLogger().debugln ("   StateMachine..", "intensity", intensity);
        int newState = IntegerEvent.STATE_EMPTY;
        if (oldEvent.isReleased()) {
            if (intensity != 0) {
                newState = IntegerEvent.STATE_PRE_STARTED;
            }
        }
        if (oldEvent.isPreStarted()) {
            if (intensity > 0 && oldIntensity > 0) {
                newState = IntegerEvent.STATE_STARTED;
            } else if (intensity < 0 && oldIntensity < 0) {
                newState = IntegerEvent.STATE_STARTED;
            } else {
                newState = IntegerEvent.STATE_RELEASED;
            }
        }
        if (oldEvent.isStarted()) {
            if (intensity > 0 && oldIntensity > 0) {
                newState = IntegerEvent.STATE_REPEATED;
            } else if (intensity < 0 && oldIntensity < 0) {
                newState = IntegerEvent.STATE_REPEATED;
            } else {
                newState = IntegerEvent.STATE_PRE_ENDED;
            }
        }
        if (oldEvent.isRepeated()) {
            if (intensity > 0 && oldIntensity > 0) {
                newState = IntegerEvent.STATE_REPEATED;
            } else if (intensity < 0 && oldIntensity < 0) {
                newState = IntegerEvent.STATE_REPEATED;
            } else {
                newState = IntegerEvent.STATE_PRE_ENDED;
            }
        }
        if (oldEvent.isPreEnded()) {
            if (intensity == 0) {
                newState = IntegerEvent.STATE_ENDED;
            } else if (intensity > 0 && oldIntensity > 0) {
                newState = IntegerEvent.STATE_ENDED;
            } else if (intensity < 0 && oldIntensity < 0) {
                newState = IntegerEvent.STATE_ENDED;
            } else {
                newState = IntegerEvent.STATE_PRE_ENDED;
            }
        }
        if (oldEvent.isEnded()) {
            if (intensity == 0) {
                newState = IntegerEvent.STATE_RELEASED;
            }
        }
        Logger.getLogger().debugln ("   StateMachine..", "newState ", newState);
        return newState;
    }

}
